package com.thingple.web.bridge.reader;

import android.content.Context;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;

import com.thingple.h5.bridge.AbstractJsBridge;
import com.thingple.h5.bridge.BridgeConfig;

@BridgeConfig(moduleName = "inventory")
public class InventoryJsBridgeImpl extends AbstractJsBridge implements InventoryJsBridge {

    private InventoryHandler inventoryHandler;

    public InventoryJsBridgeImpl(WebView webView, Context context) {
        super(webView, context);
    }

    @JavascriptInterface
    @Override
    public void startInventory(int power) {
        Log.d(getClass().getName() + "#startInventory", "Js inventory模块开始");
        Log.d(getClass().getName() + "#startInventory", "power:" + power);
        startInventory(power, null);
    }

    @JavascriptInterface
    @Override
    public void startInventory(int power, String filter) {
        Log.d(getClass().getName() + "#startInventory", "Js inventory模块开始");
        Log.d(getClass().getName() + "#startInventory", "power:" + power);
        Log.d(getClass().getName() + "#startInventory", "filter:" + filter);
        this.inventoryHandler = new InventoryHandler(getContext(), getWebView(), this, filter, power);
    }

    @JavascriptInterface
    @Override
    public void stopInventory() {
        if (inventoryHandler != null) {
            InventoryHandler ih = inventoryHandler;
            inventoryHandler = null;
            ih.destroy();
        }
    }

    @Override
    public void destroy() {
        stopInventory();
    }
}
